package cn.jinjimi.domain.data.imp;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.controller.event.CardDetailEvent;
import cn.jinjimi.app.controller.event.UploadRecordCardEvent;
import cn.jinjimi.domain.data.Store;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.CardService;
import cn.jinjimi.domain.data.dto.GreetingRecordDto;
import cn.jinjimi.domain.data.dto.HolidayCardsDto;
import cn.jinjimi.domain.store.CardStore;
import cn.jinjimi.app.entity.CardListBean;
import cn.jinjimi.app.entity.RecordCardBean;
import cn.jinjimi.app.entity.SceneCardRespData;
import cn.jinjimi.app.entity.SceneRespData;

import java.io.IOException;

import retrofit2.Response;

/**
 * Created by zhenhua on 16/7/22.
 */
public class CardStoreImp extends Store implements CardStore {


    @Override
    public CardListBean.CardBean fetchCardWithId(String cardId) throws IOException {

        Response<CardListBean> response = API.create(CardService.class).getCardWithId(cardId).execute();

        CardListBean data = parseDataAndDispatchEvents(response, new CardDetailEvent());
        return data.getGreetingCard();
    }

    @Override
    public CardListBean fetchCardsWithType(int type, int page, int limit) throws IOException {

        Response<CardListBean> response = API.create(CardService.class).getCardsWithType(type, page, limit).execute();
        CardListBean data = getHttpState(response, new CardListBean());
        return data;
    }

    @Override
    public HolidayCardsDto fetchHolidayCards(int page, int limit) throws IOException {
        Response<HolidayCardsDto> response = API.create(CardService.class).getHolidays(page, limit).execute();
        HolidayCardsDto data = getHttpState(response, new HolidayCardsDto());
        return data;
    }

    @Override
    public RecordCardBean makeCard(String blessing, String customerId, String customerName, int cardId, int holidayId, int voiceDuration) throws IOException {

        GreetingRecordDto.GreetingRecord record = new GreetingRecordDto.GreetingRecord();
        record.blessing = blessing;
        record.customer_id = Long.parseLong(StringUtils.isEmpty(customerId) ? "0" : customerId);
        record.customer_name = customerName;
        record.greeting_card_id = cardId;
        record.holiday_id = holidayId;
        record.voice_duration = voiceDuration;

        GreetingRecordDto dto = new GreetingRecordDto();
        dto.greeting_record = record;

        Response<RecordCardBean> response = API.create(CardService.class).uploadRecordCardInfo(dto).execute();
        return parseDataAndDispatchEvents(response, new UploadRecordCardEvent());
    }

    @Override
    public SceneRespData fetchScenes() throws IOException {
        Response<SceneRespData> response = API.create(CardService.class).getSceneList().execute();
        SceneRespData data = getHttpState(response, new SceneRespData());
        return data;
    }

    @Override
    public SceneCardRespData fetchSceneCards(int sceneId, int page, int limit) throws IOException {
        Response<SceneCardRespData> response = API.create(CardService.class).getSceneCardList(sceneId, page, limit).execute();
        SceneCardRespData data = getHttpState(response, new SceneCardRespData());
        return data;
    }


}
